﻿using System;
using Framework.MVC;
using Log.Model;
using Account.Model;
using Account.Model.Behavior;
using Account.IService.Entry;
using Framework.MVVM.Model;
using Account.IService;
using Account.Service;


namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IUserService), typeof(UserService), "Instance")]
    public class ValidateUserModel : AsyncModelBase,IGetUser
    {
        #region Field

        private const string m_entry = "entry";

        #endregion Field

        #region Property

        public int Old { get; set; }

        #endregion Property

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.GetUser(string.Empty,string.Empty);
        }

        #endregion AsyncModelBase

        #region IGetUser

        public string UserCode { get; set; }

        public void OnGetUserComplete(string key, UserEntry entry)
        {
            DateTime birth;
            string birthday = entry.cardID.Length==18?entry.cardID.Substring(6, 8).Insert(6, "-").Insert(4, "-"):entry.cardID.Length==15?"19"+entry.cardID.Substring(6, 6).Insert(4, "-").Insert(2, "-"):string.Empty;
            if (DateTime.TryParse(birthday, out birth)) {
                OnComplete(m_entry, DateTime.Compare(birth.AddYears(Old), DateTime.Now) < 0 ? 1:2);
                this.OnEndRequest();
                return;
            }
            OnComplete(m_entry, 0);
            this.OnEndRequest();
        }

        public void OnGetUserError(string key)
        {
            OnComplete(m_entry,0);
            this.OnEndRequest();
        }

        #endregion IGetUser
 
    }
}